| Conditions | 2 |
| Paths | 2 |
| Total Lines | 67 |
| Code Lines | 41 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | /** |
||
| 41 | } |
||
| 42 | |||
| 43 | /** |
||
| 44 | * ajaxify-form |
||
| 45 | */ |
||
| 46 | export function formToSky(userOptions = {}) |
||
| 47 | { |
||
| 48 | var options = { |
||
| 49 | selector: '.ajax-form', // selector for ajax form |
||
| 50 | }; |
||
| 51 | for (var attrname in userOptions) { |
||
| 52 | options[attrname] = userOptions[attrname]; |
||
| 53 | } |
||
| 54 | |||
| 55 | document.querySelectorAll(options.selector).forEach(item => { |
||
| 56 | if (item.querySelector('form') !== null) { |
||
| 57 | item.querySelector('form').addEventListener('submit', e => { |
||
| 58 | e.preventDefault(); |
||
| 59 | sendFormToSky(e); |
||
| 60 | }); |
||
| 61 | } |
||
| 62 | }); |
||
| 63 | |||
| 64 | var sendFormToSky = function (form) { |
||
| 65 | var $submitButton = getSubmitButton(form); |
||
| 66 | if ($submitButton !== null) { |
||
| 67 | var initialButton = getSubmitButton(form).outerHTML; |
||
| 68 | $submitButton.outerHTML = '<i class="fa fa-spinner fa-spin"></i>'; |
||
| 69 | } |
||
| 70 | |||
| 71 | //var formData = new FormData(); |
||
| 72 | var toSend = ''; |
||
| 73 | for (var i = 0; i < form.srcElement.length; ++i) { |
||
| 74 | toSend += |
||
| 75 | encodeURI(form.srcElement[i].name) + |
||
| 76 | '=' + |
||
| 77 | encodeURI(form.srcElement[i].value) + |
||
| 78 | '&'; |
||
| 79 | } |
||
| 80 | |||
| 81 | var xmlhttp = new XMLHttpRequest(); |
||
| 82 | xmlhttp.addEventListener( |
||
| 83 | 'load', |
||
| 84 | function () { |
||
| 85 | form.srcElement.outerHTML = xmlhttp.responseText; |
||
| 86 | formToSky(); |
||
| 87 | }, |
||
| 88 | false, |
||
| 89 | ); |
||
| 90 | xmlhttp.open('POST', form.srcElement.action, false); |
||
| 91 | xmlhttp.setRequestHeader( |
||
| 92 | 'Content-type', |
||
| 93 | 'application/x-www-form-urlencoded', |
||
| 94 | ); |
||
| 95 | xmlhttp.send(toSend); |
||
| 96 | }; |
||
| 97 | |||
| 98 | var renderError = function (error) { |
||
| 99 | var msg = ''; |
||
| 100 | for (var key in error) { |
||
| 101 | if (error.hasOwnProperty(key)) { |
||
| 102 | var obj = error[key]; |
||
| 103 | for (var prop in obj) { |
||
| 104 | if (obj.hasOwnProperty(prop)) { |
||
| 105 | msg += key + ' : ' + obj[prop] + '<br>'; |
||
| 106 | } |
||
| 107 | } |
||
| 108 | } |
||
| 172 |